Popular Searches
Popular Course Categories
Popular Courses

Managing alignment and spacing

Managing alignment and spacing

Flutter Layout & UI Design

Managing Alignment and Spacing in Flutter – Detailed Notes

Alignment and spacing are essential parts of Flutter UI development. Proper alignment makes an interface organized and readable, while consistent spacing improves visual hierarchy and usability. Flutter provides several widgets and properties such as MainAxisAlignment, CrossAxisAlignment, Padding, Margin, SizedBox, Spacer, Align, Center, Expanded, and Flexible for controlling the position and spacing of widgets.

Alignment and layout concepts are an important part of Flutter UI development. JustAcademy's Flutter curriculum includes Row, Column, Container, Stack, and basic UI building principles. Learn more about JustAcademy's Flutter Training.


1. What Is Alignment in Flutter?

Alignment determines where a widget or group of widgets is positioned inside the available space.

For example, a widget can be positioned:

  • At the top
  • At the bottom
  • At the center
  • At the left
  • At the right
  • At a custom position

Flutter provides different alignment mechanisms depending on the layout widget being used.


2. What Is Spacing?

Spacing is the amount of empty space between widgets or around widgets.

For example:

Column(
  children: [
    Text("Name"),
    SizedBox(height: 20),
    Text("Email"),
  ],
)

Here, SizedBox(height: 20) creates 20 logical pixels of vertical space between the two Text widgets.


3. MainAxisAlignment

MainAxisAlignment controls the position of children along the main axis of Row, Column, or Flex.

For Row

The main axis is horizontal.

Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Icon(Icons.home),
    Icon(Icons.search),
    Icon(Icons.person),
  ],
)

For Column

The main axis is vertical.

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Text("Welcome"),
    Text("Flutter"),
    ElevatedButton(
      onPressed: () {},
      child: Text("Start"),
    ),
  ],
)

4. MainAxisAlignment Values

ValueDescription
MainAxisAlignment.startPlaces children at the beginning of the main axis.
MainAxisAlignment.endPlaces children at the end of the main axis.
MainAxisAlignment.centerPlaces children in the center.
MainAxisAlignment.spaceBetweenPlaces equal space between children.
MainAxisAlignment.spaceAroundPlaces space around each child.
MainAxisAlignment.spaceEvenlyProvides equal spacing between and around children.

5. MainAxisAlignment.start

This places children at the beginning of the main axis.

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  children: [
    Text("Home"),
    Text("About"),
    Text("Contact"),
  ],
)

For a Row, the children are positioned toward the left side when using the default text direction.


6. MainAxisAlignment.end

Row(
  mainAxisAlignment: MainAxisAlignment.end,
  children: [
    Icon(Icons.search),
    Icon(Icons.person),
  ],
)

The children are positioned toward the end of the Row's main axis.


7. MainAxisAlignment.center

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Icon(Icons.check_circle, size: 60),
    SizedBox(height: 15),
    Text("Success"),
  ],
)

This is useful for creating centered empty states, success screens, welcome screens, and loading screens.


8. MainAxisAlignment.spaceBetween

spaceBetween places the first child at the beginning and the last child at the end, with equal space between the children.

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Text("Products"),
    Text("View All"),
  ],
)

This pattern is frequently used for section headers.


9. MainAxisAlignment.spaceAround

Row(
  mainAxisAlignment: MainAxisAlignment.spaceAround,
  children: [
    Icon(Icons.home),
    Icon(Icons.search),
    Icon(Icons.person),
  ],
)

Space is distributed around each child. The space at the outer edges is smaller than the total space between adjacent children.


10. MainAxisAlignment.spaceEvenly

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: [
    Text("Home"),
    Text("Search"),
    Text("Profile"),
  ],
)

This creates equal spacing between the children and the available outer edges.


11. CrossAxisAlignment

CrossAxisAlignment controls child positioning along the cross axis.

LayoutMain AxisCross Axis
RowHorizontalVertical
ColumnVerticalHorizontal

Example

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text("First Name"),
    Text("Last Name"),
    Text("Email"),
  ],
)

The children are aligned toward the start of the Column's horizontal cross axis.


12. CrossAxisAlignment Values

ValuePurpose
startAligns children toward the start of the cross axis.
endAligns children toward the end of the cross axis.
centerCenters children on the cross axis.
stretchStretches children across the cross axis when constraints permit.
baselineAligns children according to their text baselines when applicable.

13. CrossAxisAlignment.start

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text("Flutter Course"),
    Text("Learn Dart"),
    Text("Build Mobile Apps"),
  ],
)

This is commonly used for left-aligned text sections.


14. CrossAxisAlignment.center

Column(
  crossAxisAlignment: CrossAxisAlignment.center,
  children: [
    Icon(Icons.person, size: 80),
    Text("John Doe"),
    Text("Flutter Developer"),
  ],
)

This centers the children horizontally inside the Column when there is sufficient width.


15. CrossAxisAlignment.stretch

stretch can make children fill the available cross-axis extent when their constraints allow it.

Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: [
    ElevatedButton(
      onPressed: () {},
      child: Text("Login"),
    ),
    SizedBox(height: 10),
    ElevatedButton(
      onPressed: () {},
      child: Text("Register"),
    ),
  ],
)

This pattern is useful for creating full-width buttons inside a constrained Column.


16. Align Widget

The Align widget positions its child within itself according to an alignment value.

Basic Syntax

Align(
  alignment: Alignment.center,
  child: Text("Hello Flutter"),
)

Top Right Example

Align(
  alignment: Alignment.topRight,
  child: Icon(Icons.notifications),
)

Bottom Left Example

Align(
  alignment: Alignment.bottomLeft,
  child: Text("Bottom Left"),
)

17. Common Alignment Values

AlignmentPosition
Alignment.topLeftTop-left
Alignment.topCenterTop-center
Alignment.topRightTop-right
Alignment.centerLeftCenter-left
Alignment.centerCenter
Alignment.centerRightCenter-right
Alignment.bottomLeftBottom-left
Alignment.bottomCenterBottom-center
Alignment.bottomRightBottom-right

18. Center Widget

Center is a convenient widget for centering its child.

Center(
  child: Text("Centered Text"),
)

Icon Center Example

Center(
  child: Icon(
    Icons.favorite,
    size: 80,
  ),
)

19. Padding Widget

Padding adds space around a child.

Padding(
  padding: EdgeInsets.all(20),
  child: Text("Flutter Development"),
)

Padding is useful when content should not touch the edges of its parent.


20. Padding on Individual Sides

You can specify different padding values for each side using EdgeInsets.only().

Padding(
  padding: EdgeInsets.only(
    left: 20,
    top: 10,
    right: 20,
    bottom: 30,
  ),
  child: Text("Custom Padding"),
)

21. Symmetric Padding

EdgeInsets.symmetric() allows the same value to be applied to horizontal and vertical sides.

Padding(
  padding: EdgeInsets.symmetric(
    horizontal: 20,
    vertical: 10,
  ),
  child: Text("Symmetric Padding"),
)

This is useful for creating consistent screen margins.


22. Margin Using Container

Flutter does not have a separate Margin widget. The Container widget provides a margin property.

Container(
  margin: EdgeInsets.all(20),
  child: Text("Container with Margin"),
)

Margin creates space outside the Container's decoration and child area.


23. Padding vs Margin

FeaturePaddingMargin
LocationInside the widget's boundaryOutside the widget's boundary
Common WidgetPaddingContainer
PurposeSpace around the child inside a widgetSpace between the widget and surrounding content

Example

Container(
  margin: EdgeInsets.all(20),
  padding: EdgeInsets.all(16),
  color: Colors.blue,
  child: Text(
    "Flutter",
    style: TextStyle(color: Colors.white),
  ),
)

24. SizedBox for Spacing

SizedBox is one of the simplest ways to create fixed spacing.

Vertical Spacing

Column(
  children: [
    Text("Name"),
    SizedBox(height: 16),
    Text("Email"),
  ],
)

Horizontal Spacing

Row(
  children: [
    Icon(Icons.email),
    SizedBox(width: 10),
    Text("Email"),
  ],
)

25. SizedBox for Fixed Dimensions

SizedBox can also control the width and height of a child.

SizedBox(
  width: 200,
  height: 100,
  child: ElevatedButton(
    onPressed: () {},
    child: Text("Submit"),
  ),
)

26. Spacer Widget

Spacer creates flexible empty space inside a Row, Column, or Flex.

Row(
  children: [
    Text("Logo"),
    Spacer(),
    Icon(Icons.search),
    Icon(Icons.person),
  ],
)

The Spacer pushes the icons toward the end of the Row.


27. Spacer with Flex

You can use the flex property to control how much flexible space a Spacer occupies.

Row(
  children: [
    Text("Start"),
    Spacer(flex: 1),
    Text("Middle"),
    Spacer(flex: 2),
    Text("End"),
  ],
)

The flexible spaces are distributed according to their flex values.


28. Expanded for Alignment and Spacing

Expanded can distribute available space among children.

Row(
  children: [
    Expanded(
      child: Container(
        height: 80,
        color: Colors.blue,
        child: Center(
          child: Text("Item 1"),
        ),
      ),
    ),
    SizedBox(width: 10),
    Expanded(
      child: Container(
        height: 80,
        color: Colors.green,
        child: Center(
          child: Text("Item 2"),
        ),
      ),
    ),
  ],
)

29. Flexible for Adaptive Spacing

Flexible allows a child to adapt to the available space without necessarily filling all of the allocated space.

Row(
  children: [
    Flexible(
      child: Text(
        "This is a long piece of text that needs to adapt.",
      ),
    ),
    SizedBox(width: 10),
    Icon(Icons.info),
  ],
)

30. Text Alignment vs Widget Alignment

It is important to distinguish between aligning a Text widget and aligning the Text widget itself within its parent.

Text Alignment

Container(
  width: double.infinity,
  child: Text(
    "Hello Flutter",
    textAlign: TextAlign.center,
  ),
)

textAlign controls the alignment of text inside the Text widget's available width.

Widget Alignment

Align(
  alignment: Alignment.center,
  child: Text("Hello Flutter"),
)

Align controls where the Text widget itself is positioned inside its parent.


31. Creating a Well-Spaced Login Form

Padding(
  padding: EdgeInsets.all(20),
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      Text(
        "Login",
        textAlign: TextAlign.center,
        style: TextStyle(
          fontSize: 28,
          fontWeight: FontWeight.bold,
        ),
      ),
      SizedBox(height: 30),
      TextField(
        decoration: InputDecoration(
          labelText: "Email",
          border: OutlineInputBorder(),
        ),
      ),
      SizedBox(height: 16),
      TextField(
        obscureText: true,
        decoration: InputDecoration(
          labelText: "Password",
          border: OutlineInputBorder(),
        ),
      ),
      SizedBox(height: 20),
      ElevatedButton(
        onPressed: () {},
        child: Text("Login"),
      ),
    ],
  ),
)

32. Creating a Profile Card with Alignment

Container(
  padding: EdgeInsets.all(20),
  child: Row(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      CircleAvatar(
        radius: 35,
        child: Icon(Icons.person),
      ),
      SizedBox(width: 16),
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "John Doe",
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: 6),
            Text("Flutter Developer"),
            SizedBox(height: 10),
            Text("[email protected]"),
          ],
        ),
      ),
    ],
  ),
)

33. Section Header with Proper Spacing

Padding(
  padding: EdgeInsets.symmetric(horizontal: 16),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
      Text(
        "Popular Courses",
        style: TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
        ),
      ),
      TextButton(
        onPressed: () {},
        child: Text("View All"),
      ),
    ],
  ),
)

34. Creating Consistent Card Spacing

Column(
  children: [
    Card(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Text("Card 1"),
      ),
    ),
    SizedBox(height: 12),
    Card(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Text("Card 2"),
      ),
    ),
    SizedBox(height: 12),
    Card(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Text("Card 3"),
      ),
    ),
  ],
)

Consistent spacing creates a cleaner visual hierarchy.


35. Using Wrap for Spacing Between Items

When items may need to move onto multiple lines, Wrap can provide spacing using spacing and runSpacing.

Wrap(
  spacing: 10,
  runSpacing: 10,
  children: [
    Chip(label: Text("Flutter")),
    Chip(label: Text("Dart")),
    Chip(label: Text("Firebase")),
    Chip(label: Text("API")),
    Chip(label: Text("UI")),
  ],
)
  • spacing controls horizontal space between items.
  • runSpacing controls vertical space between lines.

36. Alignment Inside Container

Container provides an alignment property that can position its child.

Container(
  width: 300,
  height: 200,
  alignment: Alignment.center,
  color: Colors.blue,
  child: Text(
    "Centered Content",
    style: TextStyle(color: Colors.white),
  ),
)

Top Right Example

Container(
  width: 300,
  height: 200,
  alignment: Alignment.topRight,
  color: Colors.blue,
  child: Text("Top Right"),
)

37. Using Align with Fractional Positioning

Flutter's Alignment class can also represent custom positions.

Align(
  alignment: Alignment(0.5, -0.5),
  child: Text("Custom Position"),
)

Alignment coordinates generally range from -1 to 1:

  • -1 on the horizontal axis represents the left.
  • 0 on the horizontal axis represents the center.
  • 1 on the horizontal axis represents the right.
  • -1 on the vertical axis represents the top.
  • 0 on the vertical axis represents the center.
  • 1 on the vertical axis represents the bottom.

38. Managing Spacing in a Navigation Row

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: [
    Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(Icons.home),
        SizedBox(height: 4),
        Text("Home"),
      ],
    ),
    Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(Icons.search),
        SizedBox(height: 4),
        Text("Search"),
      ],
    ),
    Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(Icons.person),
        SizedBox(height: 4),
        Text("Profile"),
      ],
    ),
  ],
)

39. Using SafeArea for Screen Spacing

SafeArea helps keep important content away from system intrusions such as display cutouts and system UI areas.

SafeArea(
  child: Padding(
    padding: EdgeInsets.all(16),
    child: Column(
      children: [
        Text("Welcome"),
        SizedBox(height: 20),
        Text("Flutter Application"),
      ],
    ),
  ),
)

40. Alignment in Nested Layouts

In complex layouts, each parent controls its own children. For example, a Column can control vertical spacing while a nested Row controls horizontal positioning.

Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: [
    Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text("Product"),
        Text("Price"),
      ],
    ),
    SizedBox(height: 12),
    Row(
      children: [
        Expanded(
          child: Text("Flutter Course"),
        ),
        Text("₹999"),
      ],
    ),
  ],
)

41. Alignment and Spacing in Responsive Layouts

Spacing should be designed so that it remains appropriate across different screen sizes. Fixed spacing can be useful for small gaps, while Expanded, Flexible, Wrap, LayoutBuilder, and responsive constraints can help layouts adapt.

LayoutBuilder(
  builder: (context, constraints) {
    if (constraints.maxWidth >= 600) {
      return Row(
        children: [
          Expanded(child: Text("Left Content")),
          SizedBox(width: 20),
          Expanded(child: Text("Right Content")),
        ],
      );
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Text("Left Content"),
        SizedBox(height: 20),
        Text("Right Content"),
      ],
    );
  },
)

42. Common Alignment Mistakes

Mistake 1: Confusing Main Axis and Cross Axis

Remember:

  • Row → main axis is horizontal.
  • Column → main axis is vertical.

Mistake 2: Using MainAxisAlignment When CrossAxisAlignment Is Needed

Choose the property according to the direction in which you want to position the children.

Mistake 3: Adding Too Many SizedBoxes

Excessive fixed spacing can make layouts difficult to maintain.

Mistake 4: Ignoring Screen Size

A layout that looks correct on one device may overflow on another.

Mistake 5: Confusing Text Alignment with Widget Alignment

TextAlign controls text inside the Text widget, while Align positions the widget inside its parent.


43. Best Practices for Alignment and Spacing

  • Use MainAxisAlignment for main-axis positioning.
  • Use CrossAxisAlignment for cross-axis positioning.
  • Use Padding for internal spacing.
  • Use Container margin for external spacing when appropriate.
  • Use SizedBox for simple fixed gaps.
  • Use Spacer when flexible empty space is required.
  • Use Expanded and Flexible for adaptive layouts.
  • Use Wrap when content can occupy multiple lines.
  • Use consistent spacing values throughout the application.
  • Avoid unnecessary nested Containers.
  • Use SafeArea for content that should avoid system UI areas.
  • Test layouts on different screen sizes.
  • Use LayoutBuilder or other responsive techniques when the layout needs to change based on available space.

44. Practical Spacing System

A project can define a small spacing scale instead of using random values throughout the application.

const double spacingSmall = 8;
const double spacingMedium = 16;
const double spacingLarge = 24;
const double spacingExtraLarge = 32;

Example:

Column(
  children: [
    Text("Title"),
    SizedBox(height: spacingSmall),
    Text("Description"),
    SizedBox(height: spacingMedium),
    ElevatedButton(
      onPressed: () {},
      child: Text("Continue"),
    ),
    SizedBox(height: spacingLarge),
  ],
)

A centralized spacing system can make a UI easier to maintain and keep spacing consistent.


45. Alignment and Spacing Example – Complete Screen

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Alignment Example"),
        ),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text(
                      "Profile",
                      style: TextStyle(
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    IconButton(
                      onPressed: () {},
                      icon: const Icon(Icons.edit),
                    ),
                  ],
                ),
                const SizedBox(height: 30),
                const Center(
                  child: CircleAvatar(
                    radius: 50,
                    child: Icon(
                      Icons.person,
                      size: 50,
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                const Text(
                  "John Doe",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 22,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 8),
                const Text(
                  "Flutter Developer",
                  textAlign: TextAlign.center,
                ),
                const SizedBox(height: 30),
                ElevatedButton(
                  onPressed: () {},
                  child: const Text("Edit Profile"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

46. Quick Revision Table

ConceptPurpose
MainAxisAlignmentPositions children along the main axis.
CrossAxisAlignmentPositions children along the cross axis.
AlignPositions a child inside its parent.
CenterCenters a child.
PaddingAdds space inside the widget boundary around the child.
MarginAdds space outside a Container.
SizedBoxCreates fixed dimensions or spacing.
SpacerCreates flexible empty space.
ExpandedUses available flexible space.
FlexibleAllows flexible child sizing.
WrapPlaces children on multiple runs when necessary.
SafeAreaHelps keep content away from system UI intrusions.

47. Interview Questions

  1. What is the difference between MainAxisAlignment and CrossAxisAlignment?
  2. What is the main axis of a Row?
  3. What is the main axis of a Column?
  4. What is the purpose of CrossAxisAlignment.stretch?
  5. What is the difference between Padding and Margin?
  6. What is the purpose of SizedBox?
  7. What is the difference between Spacer and SizedBox?
  8. When should you use Align?
  9. What is the difference between Align and Center?
  10. What is the difference between TextAlign and Align?
  11. How does MainAxisAlignment.spaceBetween work?
  12. What is the difference between spaceAround and spaceEvenly?
  13. When should Expanded be used for spacing and layout?
  14. What is the purpose of Flexible?
  15. How can alignment and spacing be handled in responsive Flutter layouts?

48. Practice Exercises

Exercise 1 – Centered Login Screen

Create a login screen with an icon, title, email field, password field, and button. Use Column, MainAxisAlignment, CrossAxisAlignment, and SizedBox.

Exercise 2 – Profile Card

Create a profile card with an avatar on the left and user information on the right. Use Row, Column, Expanded, and SizedBox.

Exercise 3 – Product Section

Create a section header with the title on the left and a View All button on the right using spaceBetween.

Exercise 4 – Navigation Bar

Create a Row containing three or four icons with labels and use spaceEvenly for distribution.

Exercise 5 – Responsive Layout

Create a layout that uses Row on wider screens and Column on narrower screens using LayoutBuilder.


49. Learning Resources

For additional Flutter learning and practical training, visit the JustAcademy Flutter Training Course.

To register for a course demonstration, visit the JustAcademy Course Demo Registration page.


50. Summary

Managing alignment and spacing is fundamental to creating clean Flutter interfaces. MainAxisAlignment and CrossAxisAlignment control the positioning of children inside Row and Column, while Align and Center provide direct positioning control. Padding, margin, SizedBox, and Spacer help manage fixed and flexible spacing.

For adaptive interfaces, Expanded, Flexible, Wrap, and responsive layout techniques can be combined with Row and Column. Understanding these concepts allows developers to build organized forms, profiles, dashboards, product cards, navigation sections, and responsive mobile application screens.

JustAcademy's Flutter training curriculum includes Flutter widgets and UI design topics such as Row, Column, Container, Stack, and responsive UI concepts. Explore Flutter Training or Register for a Course Demo.

whatsapp